<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="预警时间" prop="warnTime">
        <el-date-picker clearable
          v-model="queryParams.warnTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择预警时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="设备编码" prop="deviceId">
        <el-input
          v-model="queryParams.deviceId"
          placeholder="请输入设备编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检测对象" prop="detection">
        <el-input
          v-model="queryParams.detection"
          placeholder="请输入检测对象"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExportMonthDangerByIndustry"
        >导出本月各行业隐患排查数据</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExportWarnByMonth"
        >导出本月企业预警情况</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExportStreetWarn"
        >导出街道预警情况</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rectifyAllList" @selection-change="handleSelectionChange">
      <el-table-column label="预警时间" align="center" prop="warnTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.warnTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备编码" align="center" prop="deviceId" />
      <el-table-column label="预警类型" align="center" prop="warnType" />
      <el-table-column label="检测对象" align="center" prop="detection" />
      <el-table-column label="检测数据单位" align="center" prop="unit" />
      <el-table-column label="检测值" align="center" prop="concentration" />
      <el-table-column label="通道信息" align="center" prop="route" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 隐患整改详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="预警时间" prop="warnTime">
          <el-date-picker clearable
            v-model="form.warnTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            readonly
            >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="设备编码" prop="deviceId">
          <el-input v-model="form.deviceId" readonly/>
        </el-form-item>
        <el-form-item label="检测对象" prop="detection">
          <el-input v-model="form.detection" readonly/>
        </el-form-item>
        <el-form-item label="检测数据单位" prop="unit">
          <el-input v-model="form.unit" readonly/>
        </el-form-item>
        <el-form-item label="检测值" prop="concentration">
          <el-input v-model="form.concentration" readonly/>
        </el-form-item>
        <el-form-item label="通道信息" prop="route">
          <el-input v-model="form.route" readonly/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <template>
            {{transformNum(form.status)}}
          </template>
        </el-form-item>
        <el-form-item label="分配人" prop="assignorName">
          <el-input v-model="form.assignorName" readonly/>
        </el-form-item>
        <el-form-item label="分配时间" prop="shareTime">
          <el-date-picker clearable
                          v-model="form.shareTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          readonly
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="整改人" prop="rectifierName">
          <el-input v-model="form.assignorName" readonly/>
        </el-form-item>
        <el-form-item label="整改详情" prop="detail">
          <el-input v-model="form.detail" readonly/>
        </el-form-item>
        <el-form-item label="整改图片" prop="image">
          <img :src="form.image">
        </el-form-item>
        <el-form-item label="整改完成时间" prop="rectifyTime">
          <el-date-picker clearable
                          v-model="form.rectifyTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          readonly
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核时间" prop="processTime">
        <el-date-picker clearable
                        v-model="form.processTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        readonly
        >
        </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRectifyAll, getRectifyAll, delRectifyAll, addRectifyAll, updateRectifyAll } from "@/api/gas/rectifyAll";

export default {
  name: "RectifyAll",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 隐患整改详情表格数据
      rectifyAllList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warnTime: null,
        deviceId: null,
        warnType: null,
        detection: null,
        unit: null,
        concentration: null,
        route: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    transformNum(num){
      switch (num) {
        case 0:
          return '待整改'
        case 1:
          return '已分配'
        case 2:
          return '待审核'
        case 3:
          return '已审核'
      }
    },
    /** 查询隐患整改详情列表 */
    getList() {
      this.loading = true;
      listRectifyAll(this.queryParams).then(response => {
        this.rectifyAllList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        warnTime: null,
        deviceId: null,
        warnType: null,
        detection: null,
        unit: null,
        concentration: null,
        route: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加隐患整改详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRectifyAll(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改隐患整改详情";
      });
    },
    /** 修改按钮操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids
      getRectifyAll(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "隐患整改详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRectifyAll(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRectifyAll(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除隐患整改详情编号为"' + ids + '"的数据项？').then(function() {
        return delRectifyAll(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gas/rectifyAll/export', {
        ...this.queryParams
      }, `rectifyAll_${new Date().getTime()}.xlsx`)
    },
    /** 导出本月各行业隐患排查数据 */
    handleExportMonthDangerByIndustry() {
      this.download('gas/rectifyAll/exportMonthDangerByIndustry',{
        ...this.queryParams
      }, `本月各行业隐患排查数据.xlsx`)
    },
    /** 导出本月企业预警情况 */
    handleExportWarnByMonth() {
      this.download('gas/rectifyAll/exportWarnByMonth',{
        ...this.queryParams
      }, `本月企业预警情况.xlsx`)
    },
    /** 导出街道预警情况 */
    handleExportStreetWarn() {
      this.download('gas/rectifyAll/exportStreetWarn',{
        ...this.queryParams
      }, `街道预警情况.xlsx`)
    }
  }
};
</script>
